<?php 
	
	class Skill_model extends CI_Model {
		
		 var $tblName = "queue_table";
		 
		function __construct()
		{
			parent::__construct();
		}
		
		function add($record)
		{
			$this->db->insert($this->tblName, $record);	// add in queue table
			$this->db->insert("qname", array("queue"=>$record['name'])); // add in qname table
			DB_modification_logs("skill");
		}
		
		function update($record)
		{
			$this->db->where('name', $record['name']);
			unset($record['name']);
			$this->db->update($this->tblName, $record);
			DB_modification_logs("skill");
		}
		
		function getDetailByID($name)
		{
		  $name = mysql_real_escape_string($name);	
		  $sql = "SELECT * FROM " . $this->tblName . " WHERE name = '" . (string)($name). "'";
		  return $this->db->query($sql)->row_array();
		}
		
		function getSkillMembers($name)
		{
			$name = trim($name);
			
			/*$sql = "select q.uniqueid, a.agent, concat(a.First_Name, ' ', a.Last_Name) as agent_name, q.penalty  from queue_member_table q INNER JOIN qagent a ON q.membername = a.agent
			where q.queue_name <> '' AND q.queue_name = '".trim($this->db->escape($name),"'")."'";*/
			
			$sql = "SELECT 
						q.uniqueid, q.membername as agent, concat(a.full_name) as agent_name, q.penalty  
					FROM
					 	queue_member_table q LEFT JOIN ccm_agents a ON q.membername = a.membername
					WHERE 
						q.queue_name <> '' AND q.queue_name = '".trim($this->db->escape($name),"'")."'";
			
			
			return $this->db->query($sql)->result();
		}
		
		
		
		function save_agent_priority($penalty_arr){
			if( count($penalty_arr) > 0 ) {
				foreach( $penalty_arr as $id=>$penalty) 
				{
					$this->db->where('uniqueid', $id);
					$this->db->update("queue_member_table", array('penalty' => intval($penalty)));
				}
			}
			DB_modification_logs("agent_priority");
		}
		
		function add_agents_to_skill($agents, $quene_name) 
		{
			$membername_arr = array();
			if( count($agents) > 0  && is_array($agents) )
			{
				foreach($agents as $membername) {
					$membername_arr[] = "'$membername'";
				}
				$members = implode(',', $membername_arr); 
				$sql_delete = " DELETE from queue_member_table where queue_name = ".$this->db->escape($quene_name)." AND queue_name <> 'dummy' AND membername NOT IN (".$members.") ";
				$this->db->query($sql_delete); 
				
				foreach($agents as $membername) 
				{
					$where = " membername  = '".mysql_real_escape_string($membername)."' and queue_name = '".mysql_real_escape_string($quene_name)."' ";
					if(! $this->common_model->record_exist("queue_member_table", $where)) // if same record exist, wouldn't need to add it
					{
						$record = array(
											'membername' => addslashes($membername),
											'queue_name' => addslashes($quene_name),
											'interface' => addslashes($membername),
											'extension' => addslashes(parseAgentExt($membername)),
										);
										
						
						//________________ Agent primary skill is set here_________________
						// If agent is first time in entered in queue member table, ( dummy is not consider ) it skill be be primary skill
						// if agent is entered in queue_member_table with some other quene/skill, that skill will be consider as priamry
						
						$where = " membername  = '".mysql_real_escape_string($membername)."' and queue_name <> 'dummy' ";
						if( $this->common_model->record_exist("queue_member_table", $where) )
						{
							$primary_skill = getAgentPrimarySkill($membername);
						}
						else
						{
							$primary_skill = $quene_name;	
						}
						
						if( $primary_skill != '' ) {
							$record['primary_skill'] = addslashes($primary_skill);
						}
									
						$this->db->insert("queue_member_table", $record);
					}
				}// end foreach
				
			}
			else
			{
				$sql_delete = " DELETE from queue_member_table where queue_name <> 'dummy' AND queue_name = ".$this->db->escape($quene_name)."";		
				$this->db->query($sql_delete); 
			}
			
			DB_modification_logs("add_agent_in_skill");
			
			
		}
		
		function getListingData($GET, $start, $offset = 20)
		{
 			$where = ' WHERE 1=1 ';
			if( isset($GET['name']) && trim($GET['name']) != '' )
			{
				$where  .= " AND name LIKE '%".trim($this->db->escape($GET['name']),"'")."%' ";	
			}
			if( isset($GET['context']) && trim($GET['context']) != '' )
			{
				$where  .= " AND context LIKE '%".trim($this->db->escape($GET['context']),"'")."%' ";	
			}
			
			$LIMIT = "LIMIT $start ,  $offset ";
			$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM " . $this->tblName . " {$where} order by name {$LIMIT} ";
			$results = $this->db->query($sql)->result();	
			$total_record = $this->common_model->get_found_rows();
			
			return array('results' => $results, 'total_record' => $total_record);
		}
		
		function getAllskills() {
			$sql = "SELECT * FROM " . $this->tblName . " order by name";
			return $this->db->query($sql)->result();
		}
		
		
  }
?>